package com.example.himalaya.views;

import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.himalaya.R;
import com.example.himalaya.adapters.PlayListAdapter;
import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.utils.LogUtils;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

public class SobPopWindow extends PopupWindow {

    private String TAG = "SobPopWindow";
    private final View mPopView;
    private View mCloseBtn;
    private RecyclerView mTrackList;
    private PlayListAdapter mPlayListAdapter;
    private ImageView mPlayModeIv;
    private TextView mPlayModeTv;
    private View mPlayModeContainer;
    private playListActionClickListener mActionClickListener = null;
    private ImageView mPlayOrderIv;
    private TextView mPlayOrderTv;
    private View mPlayOrderContainer;

    public SobPopWindow() {
        //设置宽高
        super(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
        //外部可点击：设置该属性钱先设置setBackgroundDrawable否则点击外部无法关闭
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        //加载VIew
        mPopView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.pop_play_list, null);
        //设置内容
        setContentView(mPopView);
        //设置窗口进入和退出的构造方法
        setAnimationStyle(R.style.pop_animation);
        initView();
        initEvent();
    }

    private void initEvent() {
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭popupwindow
                SobPopWindow.this.dismiss();
            }
        });
        mPlayModeContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换模式
                if (mActionClickListener != null) {
                    mActionClickListener.onPlayModeClick();
                }
            }
        });
        mPlayOrderContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //切换播放列表顺序
                mActionClickListener.onOrderClick();
            }
        });

    }

    private void initView() {
        mCloseBtn = mPopView.findViewById(R.id.play_list_close_btn);
        //1.找到控件
        mTrackList = mPopView.findViewById(R.id.play_list_rv);
        //2.设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseApplication.getAppContext());
        mTrackList.setLayoutManager(layoutManager);
        //3.设置适配器
        mPlayListAdapter = new PlayListAdapter();
        mTrackList.setAdapter(mPlayListAdapter);
        mTrackList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = UIUtil.dip2px(view.getContext(), 5);
                outRect.right = UIUtil.dip2px(view.getContext(), 5);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 5);
                // Add top margin only for the first item to avoid double space between items
                if (parent.getChildPosition(view) == 0)
                    outRect.top = UIUtil.dip2px(view.getContext(), 5);

            }
        });
//播放模式相关
        mPlayModeIv = mPopView.findViewById(R.id.play_list_play_model_iv);
        mPlayModeTv = mPopView.findViewById(R.id.play_list_play_mode_tv);
        mPlayModeContainer = mPopView.findViewById(R.id.play_list_play_mode_container);
       //播放列表顺逆序相关
        mPlayOrderIv = mPopView.findViewById(R.id.play_list_order_iv);
        mPlayOrderTv = mPopView.findViewById(R.id.play_list_order_tv);
        mPlayOrderContainer = mPopView.findViewById(R.id.play_list_play_order_container);


    }

    public void setListData(List<Track> data) {
        LogUtils.d(TAG, "data-----" + data.size());
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setData(data);
        }
    }

    public void setCurrentIndex(int index) {
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setCurrentPlayPosition(index);
            mTrackList.scrollToPosition(index);
        }
    }


    //根據當前播放模式创建状态图标

    public void updatePlayMode(XmPlayListControl.PlayMode currentModel) {
        int resId = R.drawable.selector_mode_list;
        int txId = R.string.play_mode_order_text;
        switch (currentModel) {
            case PLAY_MODEL_LIST:
                resId = R.drawable.selector_mode_list;
                txId = R.string.play_mode_order_text;
                break;
            case PLAY_MODEL_SINGLE:
                resId = R.drawable.selector_mode_single;
                txId = R.string.play_mode_single_text;
                break;
            case PLAY_MODEL_RANDOM:
                resId = R.drawable.selector_mode_random;
                txId = R.string.play_mode_random_text;
                break;
            case PLAY_MODEL_LIST_LOOP:
                resId = R.drawable.selector_mode_list_loop;
                txId = R.string.play_mode_order_loop_text;
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                resId = R.drawable.selector_mode_single_loop;
                txId = R.string.play_mode_single_loop_text;
                break;
        }
        mPlayModeIv.setImageResource(resId);
        mPlayModeTv.setText(txId);
    }


    //真正序  假逆序
    public void updateOrderIcon(boolean isOrder){
        mPlayOrderIv.setImageResource(isOrder?R.drawable.selector_order:R.drawable.selector_reverse);
        mPlayOrderTv.setText(isOrder?R.string.order:R.string.reverse);
    }

    public void setPlayListItemClickListener(PlayListItemClickListener listener) {
        mPlayListAdapter.setOnItemClickListener(listener);
    }

    public interface PlayListItemClickListener {
        void onItemClick(int position);
    }

    public void setPlayListActionClickListener(playListActionClickListener playModelistener) {
        mActionClickListener = playModelistener;
    }

    public interface playListActionClickListener {
        //播放模式
        void onPlayModeClick();

        //播放顺逆序切换
        void onOrderClick();

    }

}
